import PoolManager from "../managers/PoolManager";
import ResManager from "../managers/ResManager";
import Coordinate, { Coord } from "../type/DataStructure";
import { TileEvent, TileType } from "../type/Enum";

const {ccclass, property} = cc._decorator;

/**
 * 方块上的组件
 */
@ccclass
export default class Tile extends cc.Component {

    @property(cc.Sprite)
    // 显示图片的组件
    private sprite: cc.Sprite = null;
    /** 方块类型 */
    private _type: TileType = null;
    /** 坐标 */
    private _coord: Coordinate = null;

    onLoad() {
        this.bindTouchEvents();
    }

    onDestroy() {
        this.unbindTouchEvents();
    }

    /**
     * 重置方块
     */
    init () {
        this._type = null;
        this.sprite.spriteFrame = null;
        this.node.setScale(0);
    }

    /**
     * 节点池复用回调
     */
    public reuse() {
        this.bindTouchEvents();
    }

    /**
     * 节点池回收回调
     */
    public unuse() {
        this.unbindTouchEvents();
    }

    /**
     * 给方块绑定点击事件
     */
    private bindTouchEvents() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
    }

    /**
     * 解除方块上的事件
     */
    private unbindTouchEvents() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
    }

    /**
     * touchstart回调
     * @param event 触摸事件对象
     */
    private onTouchStart(event: cc.Event.EventTouch) {
        Global.eventTarget.emit(TileEvent.TouchStart, this._coord.copy(), event.getLocation());
    }

    /**
     * touchcancel 回调
     * @param event 触摸事件对象
     */
    private onTouchCancel(event: cc.Event.EventTouch) {
        Global.eventTarget.emit(TileEvent.TouchCancel, this._coord.copy(), event.getLocation());
    }

    /**
     * touchend 回调
     * @param event 触摸事件对象
     */
    private onTouchEnd(event: cc.Event.EventTouch) {
        Global.eventTarget.emit(TileEvent.TouchEnd, this._coord.copy(), event.getLocation());
    }

    /**
     * 设置方块的坐标
     */
    public setCoord(x: number | Coordinate, y?: number) {
        if (!this._coord) {
            this._coord = Coord();
        }
        if (typeof x === 'number') {
            this._coord.set(x, y);
        } else {
            this._coord.set(x);
        }
    }

    /**
     * 设置方块的类型，并更新方块上的图片
     * @param type 方块类型
     */
    public setType(type: TileType) {
        this._type = type;
        this.updateDisplay();
    }

    /**
     * 更新方块图片
     */
    private updateDisplay() {
        this.sprite.spriteFrame = ResManager.getTileSpriteFrame(this._type);
    }

    /**
     * 显示方块
     */
    public appear() {
        cc.tween(this.node)
            .to(0.075, {scale: 1.1})
            .to(0.025, {scale: 1})
            .start();
    }

    /**
     * 消失并回收
     */
    public disappear() {
        cc.tween(this.node)
            .to(0.1, {scale: 0})
            .call(() => PoolManager.put(this.node))
            .start();
    }
}
